package com.mondo.financialwallet.presentation.ui.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.common.ui.AbsNetworkStateFragment;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.ProductOrderListEntity;
import com.mondo.financialwallet.presentation.model.FinancialOrderList;
import com.mondo.financialwallet.presentation.ui.activity.ProductTradingStatusActivity;
import com.mondo.financialwallet.presentation.ui.adapter.FinancialOrderListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 理财订单列表
 */
public class FinancialOrderListFragment extends AbsNetworkStateFragment implements BaseQuickAdapter.OnItemClickListener {

    @Bind(R.id.financial_order_list) RecyclerView mFinancialOrderList;
    @Bind(R.id.financial_types_layout) View mFinancialTypesLayout;
    @Bind(R.id.type_select_layout) View mTypeSelectLayout;
    @Bind(R.id.income_expenditure_layout) View mIncomeExpenditureLayout;

    private FinancialOrderListAdapter mAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_financial_order_list;
    }

    @Override
    protected void initActions() {
        mAdapter = FinancialOrderListAdapter.create();
        mAdapter.setOnItemClickListener(this);
        mFinancialOrderList.setAdapter(mAdapter);

        // 理财类型选择
        mFinancialTypesLayout.setOnClickListener(v -> showFinancialTypesWindow());

        // 收入/支出
        mIncomeExpenditureLayout.setOnClickListener(v -> showIncomeExpenditureWindow());

        // 理财产品订单列表
        requestProductOrderList();
    }

    private void requestProductOrderList() {
        loadingNetwork();

        DataRepository.get().productOrderList(result -> {
            if (result.isSuccess()) {
                successNetwork();
                List<ProductOrderListEntity.DataBean> data = result.getData();
                List<FinancialOrderList> list = new ArrayList<>();
                for (ProductOrderListEntity.DataBean datum : data) {
                    String date = datum.getStatusX() == 6
                            ? datum.getClear_at() : datum.getCreate_at();

                    list.add(new FinancialOrderList(
                            datum.getStatusX(),
                            datum.getGoods_price(),
                            datum.getGoods_name(),
                            date,
                            datum.getOrder_no())
                    );
                }

                mAdapter.setNewData(list);
            } else {
                errorNetwork(result.getMsg());
            }
        });
    }

    private void showIncomeExpenditureWindow() {
//        new TypeSelectionPopupWindow(getContext(), Arrays.asList(
//                new TypeSelectionPopupWindow.Type(1,"全部"),
//                new TypeSelectionPopupWindow.Type(2,"转入"),
//                new TypeSelectionPopupWindow.Type(3,"转出")
//        )).show(mTypeSelectLayout);
    }

    private void showFinancialTypesWindow() {
//        new TypeSelectionPopupWindow(getContext(), Arrays.asList(
//                new TypeSelectionPopupWindow.Type(1,"全部"),
//                new TypeSelectionPopupWindow.Type(2,"定期理财"),
//                new TypeSelectionPopupWindow.Type(3,"余币宝"),
//                new TypeSelectionPopupWindow.Type(4,"蚂蚁搬砖"),
//                new TypeSelectionPopupWindow.Type(5,"借贷抵押")
//        )).show(mTypeSelectLayout);
    }

    @Override
    public void onAnewRequest() {
        requestProductOrderList();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ProductTradingStatusActivity.launch(getContext(), mAdapter.getItem(position));
    }
}
